// pages/article/detail.js
const app=getApp();
var common = require('../../utils/common.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    articleData:[],     //文章数据
    commentList:[],         //文章评论
    display:'none',     //是否显示评论输入框
    focus:false,        //评论输入框是否自动聚焦
    articleId:'',       //文章Id
    page:0,             //加载更多评论的起始页
    shang_show:false    //打赏弹窗
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
        title: '数据加载中...',
        mask:true,
    });
    var that=this;
    var articleId=options.articleId;
    var userId = common.getUserId();
    var token=common.getToken();
    var par = { articleId: articleId,userId:userId,token:token};
    wx.request({
        url: app.apiHost + 'article/detail',
        data: par,
        method: 'GET',
        dataType: 'JSON',
        success: function (res) {
            var data = JSON.parse(res.data);
            that.setData({
                articleData: data.content,
                commentList:data.content.commentList,
                articleId:articleId,
            })
        },
        complete:function(){
            setTimeout(()=>{wx.hideLoading()},500);
        }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
      var that=this;
      var url = `${app.apiHost}article/comment`;  //获取评论 接口
      var articleId = this.data.articleId;
      var page=++that.data.page;
      var start=page*10;
      that.setData({page:page});        //更新分页数
      var data={articleId:articleId,start:start}
      var msg='加载中';
      var func=function(par){
            let commentList = that.data.commentList.concat(par.commentList);            
            that.setData({commentList:commentList});
      };
      var res = common.request(url, data, msg, func, that);   
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
//  点击评论按饭调起键盘
  comment:function(){
    var is_login = common.checkLogin();
    if (!is_login) return;
    var that=this;
    that.setData({
        display:"block",
        focus:true,
    })
  },
//   失去焦点隐藏评论框
  hidden_comment:function(){
      var that = this;
      that.setData({          
          focus: false,
          display: "none",
      })
  },
//   发布评论
    sumit_comment:function(e){
        var is_login = common.checkLogin();
        if (!is_login) return;
        var that=this;
        var comment=e.detail.value.comment;
        var token = common.getToken();
        var userId = common.getUserId();
        var articleId=this.data.articleId;
        var data={ 
                userId: userId,
                token:token, 
                articleId:articleId, 
                commentContent:comment
            };
        
        var url=`${app.apiHost}article/addcomment`;     //发而评论接口
        var msg='发布中';
        var func=function(par){
            let url = `${app.apiHost}article/comment`;  //获取评论 接口
            let data={
                articleId: articleId                
            };            
            common.request(url,data,'',function(par1){
                
                that.setData({commentList:par1.commentList,page:0});
            },that);
        };
        var res=common.request(url,data,msg,func,that);        
    },
    // 收藏
    collect:function(e){
        var is_collect=e.target.dataset.collect;
        var is_login=common.checkLogin();
        if(!is_login) return;
        var that=this;
        var token=common.getToken();        
        var userId = common.getUserId();
        var url = `${app.apiHost}article/collection`;
        var articleId = that.data.articleId;
        if(is_collect){
            let collectId=that.data.articleData.collectId;
            if(!collectId){
                common.toast('参数错误');
                return;
            } 
            let data = { token: token, userId: userId, collectId: collectId ,articleId:articleId};
            common.request(url, data, '', function (par) {
                let data = that.data.articleData;
                data.collection = 0;
                data.collectId =0;
                --data.collectionNum;
                that.setData({
                    articleData: data,
                });
            }, that);
        }else{
            
            let data = { token: token, userId: userId, articleId: articleId };
            

            common.request(url, data, '', function (par) {
                let data = that.data.articleData;
                data.collection = 1;
                data.collectId = par.collectId;
                ++data.collectionNum;
                that.setData({
                    articleData: data,
                });
            }, that);
        }
        
    },
    //文章点赞
    zan:function(e){
        if(e.target.dataset.zan)  return;
        var is_login = common.checkLogin();
        if (!is_login) return;
        var that = this;
        var token = common.getToken();
        var userId = common.getUserId();
        var articleId = that.data.articleId;
        var data = { token: token, userId: userId, articleId: articleId };
        var url = `${app.apiHost}article/clickarticlezan`;
        
        common.request(url, data,'',function(par){
            let data=that.data.articleData;
            data.zan=1;
            ++data.zanNum;
            that.setData({
                articleData:data,
            });
        },that);
    },
    //评论点赞
    zan_comment:function(e){
        if (e.target.dataset.zan) return;
        var is_login = common.checkLogin();
        if (!is_login) return;
        var that = this;
        var token = common.getToken();
        var userId = common.getUserId();
        var articleId = that.data.articleId;
        var commentId = e.target.dataset.commentid;
        var index=e.target.dataset.index;       //评论列表下标
        var data = { token: token, userId: userId, commentId: commentId ,articleId:articleId};
        var url = `${app.apiHost}article/clickzan`;

        common.request(url, data, '', function (par) {
            let commentList = that.data.commentList;
            commentList[index].zan = 1;
            ++commentList[index].zanNum;
            that.setData({
                commentList: commentList,
            });
            common.toast('点赞成功');
        }, that);

    },
    //关注
    guanzhu: function (e) {

        var is_login = common.checkLogin();
        if (!is_login) return;

        var followUserId = e.currentTarget.dataset.followid;  //被关注者ID
        var type = e.currentTarget.dataset.type;      //是否关注
        if (type == 0) {
            type = 1;
        } else if (type == 1) {
            type = 0;
        }
        var that = this;
        var token = common.getToken();
        var userId = common.getUserId();
        var url = `${app.apiHost}bbs/follow`;   //关注接口
        var data = { token: token, userId: userId, followUserId: followUserId, type: type };

        var msg = '';
        var func = function (content) {
            let data = that.data;
            
            data.articleData.guanzhu=type;
            that.setData(data);
        };
        var res = common.request(url, data, msg, func, that);
    },
    //显示打赏弹窗
    shang_show:function(){
        var is_login = common.checkLogin();
        if (!is_login) return;
        this.setData({shang_show:true});
    },
    //隐藏打赏弹窗
    shang_hidden: function () {
        this.setData({ shang_show: false });
    },
    dashang:function(e){
        var jifen=e.currentTarget.dataset.jifen;
        var is_login = common.checkLogin();
        if (!is_login) return;
        var that = this;
        var token = common.getToken();
        var userId = common.getUserId();
        var articleId=that.data.articleId;        
        var data = { token: token, userId: userId };
        data.jifen=jifen;
        data.articleId=articleId;
        var url = `${app.apiHost}article/reward`;     //发而评论接口
        var msg = ' ';
        var func = function (content) {
            that.setData({
                shang_show: false 
            });
        };
        var res = common.request(url, data, msg, func, that,'打赏成功');   
    }
})